/**
 * Created with IntelliJ IDEA.
 * Description:手动实现一个阻塞队列【存放整数】
 * User:龙宝
 * Date:2023-12-02
 * Time:20:33
 */
class MyQueue{
    private int[] item;
    private int head;//头在哪
    private int tail;//尾在哪
    private int size;//队列中现有数据总数
    public MyQueue(int len) {
        item = new int[len];
        head = 0;
        tail = 0;
        size = 0;
    }
    //入队列
    public void put(int val) throws InterruptedException {
        synchronized (this) {
            //如果满了，就要阻塞等待
            if(size >= item.length) {
                this.wait();
            }
            //插入在尾部
            tail %= item.length;
            item[tail++] = val;
            size++;
            this.notify();
        }
    }



    //出队列
    public int take() throws InterruptedException {
        synchronized (this){
            //如果没有元素可以出了，就要阻塞等待
            if(size <= 0) {
                this.wait();
            }
            int val = item[head++];
            size--;
            this.notify();
            return val;
        }

    }

}
public class Thread11 {
    public static void main(String[] args) throws InterruptedException {
        MyQueue queue = new MyQueue(100);
        queue.put(1);
        System.out.println(queue.take());
    }

}
